/*
* Copyright (C) 2020 Tianjin KYLIN Information Technology Co., Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/&gt;.
*
*/
#include "scrollingareawidgetmajor.h"

ScrollingAreaWidgetMajor::ScrollingAreaWidgetMajor(QWidget *parent) : QWidget(parent)
{
    initMemberVariable();
    initGsetting();
    initSlideStatus();
    initLayout();
    initConnect();
}

void ScrollingAreaWidgetMajor::initMemberVariable()
{
    m_pVMainLayout = new QVBoxLayout();
    m_pVMainLayout->setContentsMargins(0, 16, 0, 16);
    m_pVMainLayout->setSpacing(0);
    this->setContentsMargins(0, 0, 0, 0);

    m_pVolumeWidget      = new QWidget();
    m_pVolumeWidget->setContentsMargins(0, 0, 0, 0);

    m_pVolumeIconLabel   = new QLabel();
    m_pVolumeIconLabel->setToolTip(tr("Volume"));
    m_pVolumeIconLabel->setProperty("useIconHighlightEffect", true);
    m_pVolumeIconLabel->setProperty("iconHighlightEffectMode", true);
    m_pVolumeIconLabel->setFixedSize(16, 16);

    m_pVolumeSlide       = new SliderButtonMajor();
    m_pVolumeSlide->installEventFilter(this);
    m_pVolumeSlide->setFixedSize(312,16);

    m_pHVolumeLayout     = new QHBoxLayout();
    m_pHVolumeLayout->setContentsMargins(0, 0, 0, 0);
    m_pHVolumeLayout->setSpacing(0);

    m_pBrightNessWidget  = new QWidget();
    m_pBrightNessWidget->setContentsMargins(0, 0, 0, 0);

    m_pBrightIconLabel   = new QLabel();
    m_pBrightIconLabel->setToolTip(tr("Bright"));
    m_pBrightIconLabel->setProperty("useIconHighlightEffect", true);
    m_pBrightIconLabel->setProperty("iconHighlightEffectMode", true);
    m_pBrightIconLabel->setFixedSize(16, 16);

    m_pBrightSlide       = new SliderButtonMajor();
    m_pBrightSlide->installEventFilter(this);
    m_pBrightSlide->setFixedSize(312,16);

    m_pBrightLayout      = new QHBoxLayout();
    m_pBrightLayout->setContentsMargins(0, 0, 0, 0);
    m_pBrightLayout->setSpacing(0);
}

void ScrollingAreaWidgetMajor::initGsetting()
{
    connect(SettingsDaemonMediaGsetting::getInstance(),&SettingsDaemonMediaGsetting::Sig_mediaGsettingChanged,
            this,&ScrollingAreaWidgetMajor::setSliderValue);

    connect(PowerModeGsetting::getInstance(),&PowerModeGsetting::Sig_powerModeChanged,
            this, &ScrollingAreaWidgetMajor::setSliderValue);

    return;
}

void ScrollingAreaWidgetMajor::initSlideStatus()
{
    m_pVolumeSlide->setValue(SettingsDaemonMediaGsetting::getInstance()->getSinkVolume());
    setVolumeStatusIcon(SettingsDaemonMediaGsetting::getInstance()->getSinkVolume());

    m_pBrightSlide->setValue(PowerModeGsetting::getInstance()->getBrightnessAc());
    setLightStatusIcon(PowerModeGsetting::getInstance()->getBrightnessAc());
    return;
}

void ScrollingAreaWidgetMajor::initLayout()
{
    m_pHVolumeLayout->addItem(new QSpacerItem(10, 8));
    m_pHVolumeLayout->addWidget(m_pVolumeIconLabel);
    m_pHVolumeLayout->addWidget(m_pVolumeSlide);
    m_pVolumeWidget->setLayout(m_pHVolumeLayout);
    m_pVolumeWidget->setFixedHeight(24);

    m_pBrightLayout->addItem(new QSpacerItem(10, 8));
    m_pBrightLayout->addWidget(m_pBrightIconLabel);
    m_pBrightLayout->addItem(new QSpacerItem(3, 1));
    m_pBrightLayout->addWidget(m_pBrightSlide);

    m_pBrightNessWidget->setLayout(m_pBrightLayout);
    m_pBrightNessWidget->setFixedHeight(24);

    m_pVMainLayout->addWidget(m_pVolumeWidget);

    // 判断亮度调节是否有效
    if (UpowerDbus::getInstance()->getCanAdjustBacklight()) {
        m_pVMainLayout->addItem(new QSpacerItem(29, 20));
        m_pVMainLayout->addWidget(m_pBrightNessWidget);
        this->setFixedSize(372,80);
    } else {
        this->setFixedSize(372,40);
    }
    this->setLayout(m_pVMainLayout);
}

void ScrollingAreaWidgetMajor::initConnect()
{
    connect(m_pVolumeSlide, &SliderButtonMajor::dataChange, this, &ScrollingAreaWidgetMajor::setVolumeSlideSlots);
    connect(m_pBrightSlide, &SliderButtonMajor::dataChange, this, &ScrollingAreaWidgetMajor::setBrightSlideSlots);
}

/* 当对应的组件修改了gsetting，需修改对应滚动条的值 */
void ScrollingAreaWidgetMajor::setSliderValue(QString key)
{
    if (key == SETTINGSDAEMON_MEDIA_VOLUME_KEY) {
        m_pVolumeSlide->setValue(SettingsDaemonMediaGsetting::getInstance()->getSinkVolume());
        setVolumeStatusIcon(SettingsDaemonMediaGsetting::getInstance()->getSinkVolume());
    } else if (key == POWER_MODE_BRIGHT_AC_KEY) {
        m_pBrightSlide->setValue(PowerModeGsetting::getInstance()->getBrightnessAc());
        setLightStatusIcon(PowerModeGsetting::getInstance()->getBrightnessAc());
    }

    if (SettingsDaemonMediaGsetting::getInstance()->getSoundState()) {
        m_pVolumeIconLabel->setPixmap(QIcon::fromTheme("audio-volume-muted-symbolic").pixmap(m_pVolumeIconLabel->size()));
    } else {
        m_pVolumeSlide->setValue(SettingsDaemonMediaGsetting::getInstance()->getSinkVolume());
        setVolumeStatusIcon(SettingsDaemonMediaGsetting::getInstance()->getSinkVolume());
    }
    return;
}

/* 根据声音大小值设置状态图标 */
void ScrollingAreaWidgetMajor::setVolumeStatusIcon(int value)
{
    if (value == 0)
        m_pVolumeIconLabel->setPixmap(QIcon::fromTheme("audio-volume-muted-symbolic").pixmap(m_pVolumeIconLabel->size()));
    else if (value > 0 && value <= 35)
        m_pVolumeIconLabel->setPixmap(QIcon::fromTheme("audio-volume-low-symbolic").pixmap(m_pVolumeIconLabel->size()));
    else if (value >= 36 && value <= 65)
        m_pVolumeIconLabel->setPixmap(QIcon::fromTheme("audio-volume-medium-symbolic").pixmap(m_pVolumeIconLabel->size()));
    else
       m_pVolumeIconLabel->setPixmap(QIcon::fromTheme("audio-volume-high-symbolic").pixmap(m_pVolumeIconLabel->size()));
    m_pVolumeIconLabel->update();
}

/* 根据亮度值设置状态图标 */
void ScrollingAreaWidgetMajor::setLightStatusIcon(int value)
{
    if (value == 0) {
        m_pBrightIconLabel->setPixmap(QIcon::fromTheme("ukui-light-0-symbolic").pixmap(m_pBrightIconLabel->size()));
    } else if (value > 0 && value <= 25) {
        m_pBrightIconLabel->setPixmap(QIcon::fromTheme("ukui-light-25-symbolic").pixmap(m_pBrightIconLabel->size()));
    } else if (value >25 && value <= 50) {
        m_pBrightIconLabel->setPixmap(QIcon::fromTheme("ukui-light-50-symbolic").pixmap(m_pBrightIconLabel->size()));
    } else if (value >50 && value <= 75) {
        m_pBrightIconLabel->setPixmap(QIcon::fromTheme("ukui-light-75-symbolic").pixmap(m_pBrightIconLabel->size()));
    } else {
        m_pBrightIconLabel->setPixmap(QIcon::fromTheme("ukui-light-100-symbolic").pixmap(m_pBrightIconLabel->size()));
    }
    m_pBrightIconLabel->update();
}

bool ScrollingAreaWidgetMajor::eventFilter(QObject *watched, QEvent *event)
{
    if (watched == m_pVolumeSlide) {
        if (event->type() == QEvent::Enter) {
                m_QFocus = "m_pVolumeSlide";
        }

    } else if (watched == m_pBrightSlide) {
        if (event->type() == QEvent::Enter) {
                m_QFocus = "m_pBrightSlide";
        }
    }
    return QWidget::eventFilter(watched,event);
}

void ScrollingAreaWidgetMajor::keyPressEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Left || event->key() == Qt::Key_Down ) {
        if (m_QFocus == "m_pVolumeSlide") {
            if (SettingsDaemonMediaGsetting::getInstance()->getSinkVolume() > 0) {
                setVolumeSlideSlots(SettingsDaemonMediaGsetting::getInstance()->getSinkVolume() - 1);
            }

        } else if (m_QFocus == "m_pBrightSlide") {
            if (PowerModeGsetting::getInstance()->getBrightnessAc() > 0) {
                setBrightSlideSlots(PowerModeGsetting::getInstance()->getBrightnessAc() - 1);
            }
        }

    } else if (event->key() == Qt::Key_Right || event->key() == Qt::Key_Up){
        if (m_QFocus == "m_pVolumeSlide") {
            if (SettingsDaemonMediaGsetting::getInstance()->getSinkVolume() != 100) {
                setVolumeSlideSlots(SettingsDaemonMediaGsetting::getInstance()->getSinkVolume() + 1);
            }
        } else if (m_QFocus == "m_pBrightSlide") {
            if (PowerModeGsetting::getInstance()->getBrightnessAc() != 100) {
                setBrightSlideSlots(PowerModeGsetting::getInstance()->getBrightnessAc() + 1);
            }
        }

    }
    QWidget::keyPressEvent(event);
}

/* 改变音量滚动条时，修改gsetting值，通知音量组件修改，同步 */
void ScrollingAreaWidgetMajor::setVolumeSlideSlots(int value)
{
    SettingsDaemonMediaGsetting::getInstance()->setSinkVolume(value);

    value == 0 ? SettingsDaemonMediaGsetting::getInstance()->setSoundState(true)
               : SettingsDaemonMediaGsetting::getInstance()->setSoundState(false);
    return;
}

/* 改变亮度滚动条时，修改gsetting值，通知修改亮度，同步 */
void ScrollingAreaWidgetMajor::setBrightSlideSlots(int value)
{
    PowerModeGsetting::getInstance()->setBrightnessAc(value);
    return;
}

SliderButtonMajor::SliderButtonMajor()
{
    updateThemeColor();

    connect(StyleGsetting::getInstance(),&StyleGsetting::Sig_styleGsettingChanged,this,[=](QString key){
        if (key == UKUI_STYLE_THEME_COLOR) {
            updateThemeColor();
            this->update();
        }
    });

    this->update();
}

SliderButtonMajor::~SliderButtonMajor()
{

}

void SliderButtonMajor::setValue(int value)
{
    // 计算比例，设置滑动条
    double original = value ;
    double targetData = original /100 * 293;

    QString str = QString::number(targetData,'f',0);
    targetData = str.toInt();
    m_touchX = targetData + HANDSLIDERSIZE;

    this->update();
}

void SliderButtonMajor::paintEvent(QPaintEvent *)
{
    QPainter paintBase(this) ;
    paintBase.setRenderHint(QPainter::Antialiasing);

    QPen pen;
    pen.setColor(QColor(0,0,0,0));

    QBrush brush;
    QColor color = qApp->palette().color(QPalette::BrightText);
    color.setAlphaF(0.1);
    brush.setColor(color);
    brush.setStyle(Qt::SolidPattern);

    paintBase.setPen(pen);
    paintBase.setBrush(brush);

    QPainterPath rectPath;
    rectPath.addRoundedRect(QRect(QPoint(0, 0), QPoint(306, 15)).adjusted(0, 0, 0, 0), 6, 6);
    paintBase.drawPath(rectPath);

    // 填充部分
    QPainter paintFiller(this);
    paintFiller.setRenderHint(QPainter::Antialiasing);
    brush.setColor(m_themeColor);
    paintFiller.setPen(pen);
    paintFiller.setBrush(brush);

    QPainterPath rectPathFiller;
    rectPathFiller.addRoundedRect(QRect(QPoint(0,0), QPoint(m_touchX, 15)).adjusted(0, 0, 0, 0), 6, 6);
    paintFiller.drawPath(rectPathFiller);

    // 中间方块部分
    QPainter paintTouch(this);
    paintTouch.setRenderHint(QPainter::Antialiasing);
    brush.setColor(QColor(255,255,255));
    paintTouch.setPen(pen);
    paintTouch.setBrush(brush);

    QPainterPath rectPathTouch;
    rectPathTouch.addRoundedRect(QRect(QPoint(m_touchX - 8,3), QPoint(m_touchX - 4, 12)).adjusted(0, 0, 0, 0), 2, 2);
    paintTouch.drawPath(rectPathTouch);

}

void SliderButtonMajor::mouseMoveEvent(QMouseEvent *event)
{
    updatePaint(event->x());
    this->update();
}

void SliderButtonMajor::mousePressEvent(QMouseEvent *event)
{
    updatePaint(event->x());
    this->update();
}

void SliderButtonMajor::updatePaint(int value)
{
    if (value < 13) {
        m_touchX = 13;
        proportionHandle(13);
    } else if (value > 306) {
        m_touchX = 306;
        proportionHandle(306);
    } else {
        proportionHandle(value);
        m_touchX = value;
    }
}

void SliderButtonMajor::proportionHandle(int value)
{
    // 计算比例，发送信号
    double original = value - HANDSLIDERSIZE;
    double targetData = original / 293 *100;
    QString str = QString::number(targetData,'f',0);
    targetData = str.toInt();

    // 对比相同数据，防止重复设置数据
    if(m_saveData == targetData){
        return;
    }

    emit dataChange(targetData);
    m_saveData = targetData;
}

void SliderButtonMajor::updateThemeColor()
{
    int r,g,b;
    m_themeColor = qApp->palette().color(QPalette::Highlight);
    m_themeColor.getRgb(&r,&g,&b);
}

